package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-mgr/cache"
	"novel-mgr/models"
	"novel-mgr/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}
	trach := "redis_log_" + username
	loginContent, _ := cache.Rdb.Get(cache.Ctx, trach).Int()
	if loginContent >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "登录失败三次，禁止登录",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, trach)
		if loginContent == 2 {
			cache.Rdb.Set(cache.Ctx, trach, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "登录已上锁，两小时后解锁",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "登录失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"user": user,
	})
}
func List(c *gin.Context) {
	var arr []models.Novel
	redis_key := "redis_key_list"
	RdisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if RdisData == "" {
		models.DB.Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RdisData), &arr)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"arr":  arr,
	})

}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var arr models.Novel
	/*
		SELECT *FROM
		novel_cons
		LEFT JOIN novels ON novel_cons.nid=novels.id
		WHERE novels.id=1
	*/
	tra_key := "trac_detail" + id
	RdisData, _ := cache.Rdb.Get(cache.Ctx, tra_key).Result()
	if RdisData == "" {
		models.DB.Table("novel_cons").
			Joins("LEFT JOIN novels ON novel_cons.nid=novels.id").
			Where("novels.id=?", id).Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, tra_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RdisData), &arr)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "详情成功",
		"arr":  arr,
	})
}
func Content(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	content := c.PostForm("content")
	data := models.NovelCon{

		Uid:          uid,
		Nid:          nid,
		NovelContent: content,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "评论失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "评论成功",
	})
	return
}
func Detaild(c *gin.Context) {
	id := c.Query("id")
	var arr models.Novel

	redis_key := "redis_key_list" + id
	RdisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if RdisData == "" {
		models.DB.Where("id=?", id).Order("id DESC").Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RdisData), &arr)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"arr":  arr,
	})

	return

}
